package com.youyi.yyfilepickerlibrary.core.util;

import android.content.ContentUris;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.youyi.yychosesdk.constant.Type;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;


public class ConvertUtils {
    public static final long GB = 1073741824;
    public static final long KB = 1024;
    public static final long MB = 1048576;

    public static int toShort(byte b, byte b2) {
        return (b << 8) + (b2 & -1);
    }

    public static int toInt(Object obj) {
        try {
            return Integer.parseInt(obj.toString());
        } catch (NumberFormatException unused) {
            return -1;
        }
    }

    public static int toInt(byte[] bArr) {
        int i = 0;
        for (int i2 = 0; i2 < bArr.length; i2++) {
            i += (bArr[i2] & -1) << (i2 * 8);
        }
        return i;
    }

    public static long toLong(Object obj) {
        try {
            return Long.parseLong(obj.toString());
        } catch (NumberFormatException unused) {
            return -1;
        }
    }

    public static float toFloat(Object obj) {
        try {
            return Float.parseFloat(obj.toString());
        } catch (NumberFormatException unused) {
            return -1.0f;
        }
    }

    public static byte[] toByteArray(int i) {
        return ByteBuffer.allocate(4).putInt(i).array();
    }

    public static byte[] toByteArray(String str, boolean z) {
        if (str == null || str.equals("")) {
            return null;
        }
        if (!z) {
            return str.getBytes();
        }
        String replaceAll = str.replaceAll("\\s+", "");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(replaceAll.length() / 2);
        for (int i = 0; i < replaceAll.length(); i += 2) {
            byteArrayOutputStream.write(("0123456789ABCDEF".indexOf(replaceAll.charAt(i)) << 4) | "0123456789ABCDEF".indexOf(replaceAll.charAt(i + 1)));
        }
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            LogUtils.warn(e);
        }
        return byteArray;
    }

    public static String toHexString(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (byte b : str.getBytes()) {
            sb.append(Integer.toHexString(b & -1));
            sb.append(" ");
        }
        return sb.toString();
    }


    public static String toHexString(byte... bArr) {
        char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] cArr2 = new char[(bArr.length * 2)];
        int i = 0;
        for (int i2 = 0; i2 < bArr.length; i2++) {
            int i3 = bArr[i2] < 0 ? bArr[i2] + 256 : bArr[i2];
            int i4 = i + 1;
            cArr2[i] = cArr[(i3 == 1 ? 1 : 0) >>> 4];
            i = i4 + 1;
            cArr2[i4] = cArr[(i3 == 1 ? 1 : 0) & 15];
        }
        return new String(cArr2);
    }

    public static String toHexString(int i) {
        String hexString = Integer.toHexString(i);
        LogUtils.verbose(String.format(Locale.CHINA, "%d to hex string is %s", Integer.valueOf(i), hexString));
        return hexString;
    }

    /* JADX DEBUG: Failed to insert an additional move for type inference into block B:11:0x001c */
    /* JADX DEBUG: Multi-variable search result rejected for r7v0, resolved type: byte[] */
    /* JADX DEBUG: Multi-variable search result rejected for r4v1, resolved type: byte */
    /* JADX DEBUG: Multi-variable search result rejected for r4v2, resolved type: int */
    /* JADX DEBUG: Multi-variable search result rejected for r4v5, resolved type: byte */
    /* JADX DEBUG: Multi-variable search result rejected for r4v6, resolved type: byte */
    /* JADX DEBUG: Multi-variable search result rejected for r4v7, resolved type: int */
    /* JADX WARN: Multi-variable type inference failed */
    public static String toBinaryString(byte... bArr) {
        char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] cArr2 = new char[(bArr.length * 8)];
        int i = 0;
        for (int i2 = 0; i2 < bArr.length; i2++) {
            int i3 = bArr[i2] < 0 ? bArr[i2] + 256 : bArr[i2];
            int i4 = i + 1;
            cArr2[i] = cArr[((i3 == 1 ? 1 : 0) >>> 7) & 1];
            int i5 = i4 + 1;
            cArr2[i4] = cArr[(i3 >>> 6) & 1];
            int i6 = i5 + 1;
            cArr2[i5] = cArr[(i3 >>> 5) & 1];
            int i7 = i6 + 1;
            cArr2[i6] = cArr[(i3 >>> 4) & 1];
            int i8 = i7 + 1;
            cArr2[i7] = cArr[(i3 >>> 3) & 1];
            int i9 = i8 + 1;
            cArr2[i8] = cArr[(i3 >>> 2) & 1];
            int i10 = i9 + 1;
            cArr2[i9] = cArr[(i3 >>> 1) & 1];
            i = i10 + 1;
            cArr2[i10] = cArr[(i3 == 1 ? 1 : 0) & 1];
        }
        return new String(cArr2);
    }

    public static String toBinaryString(int i) {
        String binaryString = Integer.toBinaryString(i);
        LogUtils.verbose(String.format(Locale.CHINA, "%d to binary string is %s", Integer.valueOf(i), binaryString));
        return binaryString;
    }

    public static String toSlashString(String str) {
        char[] charArray = str.toCharArray();
        String str2 = "";
        for (char c : charArray) {
            if (c == '\"' || c == '\'' || c == '\\') {
                str2 = str2 + "\\";
            }
            str2 = str2 + c;
        }
        return str2;
    }

    public static <T> T[] toArray(List<T> list) {
        return (T[]) list.toArray();
    }

    public static <T> List<T> toList(T[] tArr) {
        return Arrays.asList(tArr);
    }

    public static String toString(Object[] objArr) {
        return Arrays.deepToString(objArr);
    }

    public static String toString(Object[] objArr, String str) {
        StringBuilder sb = new StringBuilder();
        for (Object obj : objArr) {
            sb.append(obj);
            sb.append(str);
        }
        return sb.toString();
    }

    public static byte[] toByteArray(InputStream inputStream) {
        if (inputStream == null) {
            return null;
        }
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[100];
            while (true) {
                int read = inputStream.read(bArr, 0, 100);
                if (read == -1) {
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    byteArrayOutputStream.close();
                    inputStream.close();
                    return byteArray;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        } catch (IOException e) {
            LogUtils.warn(e);
            return null;
        }
    }

    public static byte[] toByteArray(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            LogUtils.warn(e);
        }
        return byteArray;
    }

    public static Bitmap toBitmap(byte[] bArr, int i, int i2) {
        if (bArr.length == 0) {
            return null;
        }
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = false;
            options.inPreferredConfig = null;
            if (i > 0 && i2 > 0) {
                options.outWidth = i;
                options.outHeight = i2;
            }
            Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            decodeByteArray.setDensity(96);
            return decodeByteArray;
        } catch (Exception e) {
            LogUtils.error(e);
            return null;
        }
    }

    public static Bitmap toBitmap(byte[] bArr) {
        return toBitmap(bArr, -1, -1);
    }

    public static Bitmap toBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        if (drawable instanceof ColorDrawable) {
            Bitmap createBitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
            new Canvas(createBitmap).drawColor(((ColorDrawable) drawable).getColor());
            return createBitmap;
        } else if (!(drawable instanceof NinePatchDrawable)) {
            return null;
        } else {
            Bitmap createBitmap2 = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(createBitmap2);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            drawable.draw(canvas);
            return createBitmap2;
        }
    }

    /* JADX INFO: Can't fix incorrect switch cases order, some code will duplicate */
    public static String toPath(Context context, Uri uri) {
        if (uri == null) {
            LogUtils.verbose("uri is null");
            return "";
        }
        LogUtils.verbose("uri: " + uri.toString());
        String path = uri.getPath();
        String scheme = uri.getScheme();
        String authority = uri.getAuthority();
        Uri uri2 = null;
        if ((Build.VERSION.SDK_INT >= 19) && DocumentsContract.isDocumentUri(context, uri)) {
            String documentId = DocumentsContract.getDocumentId(uri);
            String[] split = documentId.split(":");
            String str = split[0];
            authority.hashCode();
            char c = 65535;
            switch (authority.hashCode()) {
                case 320699453:
                    if (authority.equals("com.android.providers.downloads.documents")) {
                        c = 0;
                        break;
                    }
                    break;
                case 596745902:
                    if (authority.equals("com.android.externalstorage.documents")) {
                        c = 1;
                        break;
                    }
                    break;
                case 1734583286:
                    if (authority.equals("com.android.providers.media.documents")) {
                        c = 2;
                        break;
                    }
                    break;
            }
            switch (c) {
                case 0:
                    return _queryPathFromMediaStore(context, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId).longValue()), null, null);
                case 1:
                    if ("primary".equalsIgnoreCase(str)) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }
                    break;
                case 2:
                    if ("image".equals(str)) {
                        uri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if (Type.VIDEO.equals(str)) {
                        uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(str)) {
                        uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }
                    return _queryPathFromMediaStore(context, uri2, "_id=?", new String[]{split[1]});
            }
        } else if ("content".equalsIgnoreCase(scheme)) {
            if (authority.equals("com.google.android.apps.photos.content")) {
                return uri.getLastPathSegment();
            }
            return _queryPathFromMediaStore(context, uri, null, null);
        } else if ("file".equalsIgnoreCase(scheme)) {
            return uri.getPath();
        }
        LogUtils.verbose("uri to path: " + path);
        return path;
    }

    private static String _queryPathFromMediaStore(Context context, Uri uri, String str, String[] strArr) {
        try {
            Cursor query = context.getContentResolver().query(uri, new String[]{"_data"}, str, strArr, null);
            if (query == null) {
                return null;
            }
            int columnIndexOrThrow = query.getColumnIndexOrThrow("_data");
            query.moveToFirst();
            String string = query.getString(columnIndexOrThrow);
            query.close();
            return string;
        } catch (IllegalArgumentException e) {
            LogUtils.error(e);
            return null;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:15:0x005b  */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x0068 A[RETURN] */
    /* JADX WARNING: Removed duplicated region for block: B:19:0x0069  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static Bitmap toBitmap(android.view.View r7) {
        /*
        // Method dump skipped, instructions count: 169
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yyfilepickerlibrary.core.util.ConvertUtils.toBitmap(android.view.View):android.graphics.Bitmap");
    }

    public static Drawable toDrawable(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        return new BitmapDrawable(Resources.getSystem(), bitmap);
    }

    public static byte[] toByteArray(Drawable drawable) {
        return toByteArray(toBitmap(drawable));
    }

    public static Drawable toDrawable(byte[] bArr) {
        return toDrawable(toBitmap(bArr));
    }

    public static int toPx(Context context, float f) {
        int i = (int) ((context.getResources().getDisplayMetrics().density * f) + 0.5f);
        LogUtils.verbose(f + " dp == " + i + " px");
        return i;
    }

    public static int toDp(Context context, float f) {
        int i = (int) ((f / context.getResources().getDisplayMetrics().density) + 0.5f);
        LogUtils.verbose(f + " px == " + i + " dp");
        return i;
    }

    public static int toSp(Context context, float f) {
        int i = (int) ((f / context.getResources().getDisplayMetrics().scaledDensity) + 0.5f);
        LogUtils.verbose(f + " px == " + i + " sp");
        return i;
    }

    public static String toGbk(String str) {
        try {
            return new String(str.getBytes("utf-8"), "gbk");
        } catch (UnsupportedEncodingException e) {
            LogUtils.warn(e);
            return str;
        }
    }

    public static String toFileSizeString(long j) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if (j < 1024) {
            return j + "B";
        } else if (j < 1048576) {
            return decimalFormat.format(((double) j) / 1024.0d) + "K";
        } else if (j < GB) {
            return decimalFormat.format(((double) j) / 1048576.0d) + "M";
        } else {
            return decimalFormat.format(((double) j) / 1.073741824E9d) + "G";
        }
    }

    public static String toString(InputStream inputStream, String str) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, str));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                sb.append(readLine);
                sb.append("\n");
            }
            bufferedReader.close();
            inputStream.close();
        } catch (IOException e) {
            LogUtils.error(e);
        }
        return sb.toString();
    }

    public static String toString(InputStream inputStream) {
        return toString(inputStream, "utf-8");
    }

    public static int toDarkenColor(int i) {
        return toDarkenColor(i, 0.8f);
    }

    public static int toDarkenColor(int i, float f) {
        float[] fArr = new float[3];
        Color.colorToHSV(i, fArr);
        fArr[2] = fArr[2] * f;
        return Color.HSVToColor(fArr);
    }

    public static String toColorString(int i) {
        return toColorString(i, false);
    }

    public static String toColorString(int i, boolean z) {
        String hexString = Integer.toHexString(Color.alpha(i));
        String hexString2 = Integer.toHexString(Color.red(i));
        String hexString3 = Integer.toHexString(Color.green(i));
        String hexString4 = Integer.toHexString(Color.blue(i));
        if (hexString.length() == 1) {
            hexString = "0" + hexString;
        }
        if (hexString2.length() == 1) {
            hexString2 = "0" + hexString2;
        }
        if (hexString3.length() == 1) {
            hexString3 = "0" + hexString3;
        }
        if (hexString4.length() == 1) {
            hexString4 = "0" + hexString4;
        }
        if (z) {
            String str = hexString + hexString2 + hexString3 + hexString4;
            LogUtils.verbose(String.format(Locale.CHINA, "%d to color string is %s", Integer.valueOf(i), str));
            return str;
        }
        String str2 = hexString2 + hexString3 + hexString4;
        LogUtils.verbose(String.format(Locale.CHINA, "%d to color string is %s%s%s%s, exclude alpha is %s", Integer.valueOf(i), hexString, hexString2, hexString3, hexString4, str2));
        return str2;
    }

    public static ColorStateList toColorStateList(int i, int i2, int i3, int i4) {
        int[] iArr = {i2, i3, i, i3, i4, i};
        return new ColorStateList(new int[][]{new int[]{16842919, 16842910}, new int[]{16842910, 16842908}, new int[]{16842910}, new int[]{16842908}, new int[]{16842909}, new int[0]}, iArr);
    }

    public static ColorStateList toColorStateList(int i, int i2) {
        return toColorStateList(i, i2, i2, i);
    }
}
